package kr.co.redcircle.broker.handler.name;

import kr.co.redcircle.broker.Broker;
import kr.co.redcircle.broker.io.BrokerRequest;
import kr.co.redcircle.broker.io.BrokerResponse;
import kr.co.redcircle.broker.io.IHandler;
import kr.co.redcircle.broker.registry.Registry;
import kr.co.redcircle.broker.utility.JsonUtility;
import net.sf.json.JSONObject;

public class GetHostResponseHandler implements IHandler {

	@Override
	public BrokerResponse invoke(String respString) {
		BrokerRequest jObj = JsonUtility.getBrokerRequest(respString);
		System.out.println("GetHostResponseHandler~! : " + respString);
		BrokerRequest request = new BrokerRequest();

		JSONObject hostInfo = new JSONObject();
		hostInfo.put(Broker.IP, Registry.getHOST());
		hostInfo.put(Broker.PORT, Registry.getPORT());		
		hostInfo.put(Broker.CID_S, Registry.getCID());		
		
		JSONObject params = new JSONObject();
		params.put(Broker.HOST, hostInfo);
		params.put(Broker.RESULT, true);
		
		request.put(Broker.FROM, jObj.get(Broker.TO));
		request.put(Broker.TO, jObj.get(Broker.FROM));
		request.put(Broker.BUSINESS, "nameNodeInformation");
		request.put(Broker.METHOD, "setup");		
		request.put(Broker.PARAMETERS, params);
		
		Registry.send(Broker.MULTICAST, request);
		
		return null;
	}

}
